package com.yzy.fly;

import com.yzy.fly.util.FileUtil;
import com.yzy.fly.util.PropertiesUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.configuration2.PropertiesConfiguration;
import org.apache.commons.configuration2.convert.DefaultListDelimiterHandler;
import org.apache.commons.configuration2.convert.ListDelimiterHandler;
import org.apache.commons.configuration2.ex.ConfigurationException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.ApplicationContextException;
import org.springframework.core.io.ClassPathResource;

import java.io.*;
import java.util.Properties;

import static java.nio.charset.StandardCharsets.UTF_8;

@Slf4j
public class FlyProperties {

    public static Properties build(String fileName) throws IOException {
        ClassPathResource resource = new ClassPathResource(fileName);
        return build(resource.getFile());
    }

    public static Properties build(File file) throws FileNotFoundException {
        return build(new FileInputStream(file));
    }

    public static Properties build(InputStream inputStream) {
        Properties properties = loadToEnvMap(inputStream);
        FlyEnvContext.add("applicationProperties", properties);
        return properties;
    }

    public static void buildActive(String active) throws IOException {
        log.info("spring.profiles.active: {}", active);
        String fileName = "application-" + active + ".properties";
        ClassPathResource resource = new ClassPathResource(fileName);
        buildActive(resource.getInputStream());
    }

    public static void buildActive(File file) throws FileNotFoundException {
        buildActive(new FileInputStream(file));
    }

    public static void buildActive(InputStream inputStream) {
        Properties properties = loadToEnvMap(inputStream);
        FlyEnvContext.add("applicationProperties", properties);
    }

    public static Properties loadToEnvMap(InputStream inputStream) {
        Properties properties = new Properties();
        try {
            PropertiesUtil.load(properties, inputStream, FileUtil.Charset.UTF_8);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ApplicationContextException("未找到,请检查");
        }
        FlyEnvContext.add("applicationProperties", properties);
        return properties;
    }

    public static void buildPropertiesConfiguration(String fileName) throws IOException {
        ClassPathResource resource = new ClassPathResource(fileName);
        buildPropertiesConfiguration(resource.getFile());
    }

    public static void buildPropertiesConfiguration(File file) throws FileNotFoundException {
//        FileReader reader = new FileReader(file, UTF_8);
//        buildPropertiesConfiguration(reader);
        buildPropertiesConfiguration(new FileInputStream(file));
    }

    public static void buildPropertiesConfiguration(InputStream inputStream) {
        Reader reader = new InputStreamReader(inputStream, UTF_8);
        buildPropertiesConfiguration(reader);
    }

    public static void buildPropertiesConfiguration(Reader reader) {
        PropertiesConfiguration config = new PropertiesConfiguration();
        ListDelimiterHandler listDelimiterHandler = new DefaultListDelimiterHandler(',');
        config.setListDelimiterHandler(listDelimiterHandler);
        try {
            config.read(reader);
        } catch (ConfigurationException e) {
            e.printStackTrace();
            throw new ApplicationContextException("解析错误,请检查");
        } catch (IOException e) {
            e.printStackTrace();
            throw new ApplicationContextException("未找到,请检查");
        }
        FlyEnvContext.add("applicationPropertiesPro", config);
    }

}
